package com.stylefeng.guns.rest.modular.auth.controller;

import com.alibaba.fastjson.JSONObject;

import com.stylefeng.guns.rest.common.persistence.model.LiveAccount;
import com.stylefeng.guns.rest.common.persistence.model.LiveAccountBO;
import com.stylefeng.guns.rest.modular.applications.service.IUsersService;
import com.stylefeng.guns.rest.modular.auth.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


/**
 * @ClassName: RederController
 * @date 2018年9月7日
 */
@Controller
@RequestMapping("/user")
public class UserController {

    private final static Logger LOGGER = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private IUsersService usersService;


    /**
     * 登录
     */
    @PostMapping("/login")
    @ResponseBody
    public Object login(@RequestParam(value = "account", required = true) String account,
                        @RequestParam(value = "password", required = true) String password,
                        @RequestHeader(value = "device") String device) {
        Object jsonObject = new JSONObject();
        try {
            jsonObject = usersService.login(account, password,device);
            return ResponseEntity.ok(jsonObject);
        } catch (Exception e) {
            return ResponseEntity.ok(ResultUtil.error(e.getMessage()));
        }
    }


    //根据token获取会员信息
    @RequestMapping(value = "/tokenLogin")
    @ResponseBody
    public ResponseEntity tokenLogin(@RequestHeader(value = "token", required = true) String token,
                                     @RequestHeader(value = "device") String device) {
        Object object = null;
        try {
            object = usersService.tokenLogin(token,device);
            return ResponseEntity.ok(object);
        } catch (Exception e) {
            return ResponseEntity.ok(ResultUtil.error(e.getMessage()));
        }
    }

    //支付回调函数执行
    /*public Object payCallback(){

        return ;
    }*/

    /**
     * @Title: registerReder @Description: 注册读者
     * 参数 @return Object 返回类型 @throws
     */
    @PostMapping("/register")
    @ResponseBody
    public Object registerReder(LiveAccount account) {
        Object jsonObject = new JSONObject();
        try {
            jsonObject = usersService.register(account);
            return ResponseEntity.ok(jsonObject);
        } catch (Exception e) {
            return ResponseEntity.ok(ResultUtil.error(e.getMessage()));
        }
    }


    /**
     * @Title: changePassword @Description: 修改密码
     * 参数 @return Object 返回类型 @throws
     */
    @PostMapping("/changePassword")
    @ResponseBody
    public Object changePassword(@RequestParam(value = "oldPassword", required = true) String oldPassword,
                                 @RequestParam(value = "newPassword", required = true) String newPassword,
                                 @RequestHeader(value = "token", required = true) String token) {
        Object jsonObject = new JSONObject();
        try {
            jsonObject = usersService.changePassword(oldPassword, newPassword, token);
            return ResponseEntity.ok(jsonObject);
        } catch (Exception e) {
            return ResponseEntity.ok(ResultUtil.error(e.getMessage()));
        }

    }

    /**
     * @Title: resetPassword @Description: 重置密码
     * 参数 @return Object 返回类型 @throws
     */
    @PostMapping("/resetPassword")
    @ResponseBody
    public Object resetPassword(@RequestParam(value = "account", required = true) String account,
                                @RequestParam(value = "newPassword", required = true) String newPassword,
                                @RequestParam(value = "code", required = true) String code) {
        Object jsonObject = new JSONObject();
        try {
            jsonObject = usersService.resetPassword(code, account, newPassword);
            return ResponseEntity.ok(jsonObject);
        } catch (Exception e) {
            return ResponseEntity.ok(ResultUtil.error(e.getMessage()));
        }
    }


    //校验是否Vip
    @RequestMapping(value = "/isVip")
    @ResponseBody
    public ResponseEntity isVip(@RequestHeader(value = "token", required = true) String token) {
        try {
            Object object = usersService.isVip(token);
            return ResponseEntity.ok(object);
        } catch (Exception e) {
            return ResponseEntity.ok(ResultUtil.error(e.getMessage()));
        }
    }


    /**
     * @Title: sendVerificationCode @Description: 给手机号发送验证码
     * 参数 @return Object 返回类型 @throws
     */
    @PostMapping("/sendVerificationCode")
    @ResponseBody
    public Object sendVerificationCode(@RequestParam(value = "account", required = true) String account,
                                       @RequestParam(value = "type", required = true) String type) {
        Object jsonObject = new JSONObject();
        try {
            jsonObject = usersService.sendVerificationCode(account, type);
            return ResponseEntity.ok(jsonObject);
        } catch (Exception e) {
            return ResponseEntity.ok(ResultUtil.error(e.getMessage()));
        }
    }


}
